// var  baseUrl = 'http://joqiuyf.etjbooks.com.cn:8021/';
var  baseUrl = 'https://activity.etjbooks.com.cn/';

var loading;
var mySwiper;
function ajax(requestUrl,method,requestData,SuccessCallback){
        var str = '';
        if(Object.keys(requestData).length !== 0) {
            str = JSON.stringify(requestData);
        }
        // console.log(str)
        // loading = layer.load(5);
        $.ajax({
            type: method,
            url: baseUrl + requestUrl,
            data: requestData,
            contentType: "application/json",
            dataType: "json",
            sync: false,
            success: function(data) {
                layer.close(loading);
                var obj = null;
                try {
                    obj = eval('(' + data + ')');
                } catch (ex) {
                    obj = data;
                }
                if(obj.status == 200) {
                    SuccessCallback(obj.data);
                }
             },
            error: function(err) {
                // layer.msg(err.responseJSON.msg);
            },
            complete: function(XHR, TS) {
                XHR = null
            }
        });
}

function splitDigits(array, groupSize = 6) {
    // const digits = Math.abs(num).toString().split('').map(Number);
    const groups = [];
    let index = 0;
    while (index < array.length)  {
        groups.push(array.slice(index,  index += groupSize));
    }
    return groups;
}


function  getCover(array) {
    var url = ''
    var cover = '';
    var type;
    var file_vframe
    // console.log(array)
    $.each(array,function (i,v) {
        if(v.ext == "jpg" || v.ext == "png") {
            cover = v.file_url;
            file_vframe = v.file_vframe
            return false;
        }
        if(v.ext == "mp4") {
            cover = v.file_vframe;
            file_vframe = v.file_vframe
            return false;
        }
    })
    return {
        url:file_vframe,
        cover:cover
    };
}


$(function (){
    window.arrow = true;
    var mySwiper = new Swiper('.swiper-container',{
        loop: true,
        speed:1000,
        autoplay: {
            delay: 5000,//1秒切换一次
            disableOnInteraction: false,
        }
    })
    var obj = {
        topic:0,
        page_count:60,
        page:1
    }
    addSwiper();

    $(".headBox").click(function (){

        if($(".navBox").hasClass('hide')) {
            $(".navBox").removeClass('hide')
        } else {
            $(".navBox").addClass('hide')

        }
    })


    document.getElementById("scrollBtn").onmousedown = function () {
        clearInterval(window.time);
        window.time = setInterval(function () {
            document.getElementById('navContent').scrollLeft += 18;
        },10)
    };
    // if(!window.arrow) {return;}
    document.getElementById("scrollBtn").onmouseup = function () {
        // if(!window.arrow) {return;}
        clearInterval(window.time);
    }

    document.getElementById("scrollBtn").addEventListener('touchstart',function () {
        clearInterval(window.time);
        window.time = setInterval(function () {
            document.getElementById('navContent').scrollLeft += 18;
        },10)
    },false);

    document.getElementById("scrollBtn").addEventListener('touchend',function () {
        clearInterval(window.time);
    },false);

    $(".scrollBtn").click(function () {
          $(".navContent").scrollTo(x-coord,);
    })

    $(".nav").click(function (){
        $(".nav").removeClass('active');
        $(this).addClass('active');
        var num = $(this).attr('data-type');
        obj.topic = num;
        obj.page = 1;
        mySwiper.removeAllSlides();
        addSwiper();
    })


    $("body").on('click','.item-i',function (){
        var id = $(this).attr('data-id');
        ajax('ajax_article/'+ id,'get',{},function (res){
            var data = res[0];
            console.log(data)
            if(data.ext == 'mp4') {
                 $("#dialogVideo").attr('src',data.file_url)
                 $("#vidoeDialog").removeClass('hide');
                mySwiper.autoplay.stop();
            } else {
                $("#dialogImg").attr('src',data.file_url);
                $("#imgDialog").removeClass('hide');
                mySwiper.autoplay.stop();
            }
        })
        return false;
    })

    $(".mark").click(function (){
        $(".dialog").addClass('hide');
        mySwiper.autoplay.start();
    })

    function addSwiper() {
        ajax('ajax_article_resource','get',obj,function (res){
            var html = '';
            var data = res.data;
            var array = splitDigits(data,6);
            //console.log(array);

            $.each(array,function (i,v) {
                html += '<div class="swiper-slide type2">';
                $.each(v,function (x,y) {
                    var imgurl = getCover(y.file_list);
                    // console.log(imgurl)
                    html += ' <a class="item item-i imgBox" data-id="'+ y.id +'" style="background-image: url('+ imgurl.cover +')">\n' +
                        '                      <div class="bottomTtitle">' + y.title + '</div>' +
                        '                  </a>'
                })
                html += '</div>';
            })

            mySwiper.appendSlide(html);
            mySwiper.slideTo(1, 0, false);
            obj.page ++ ;
            if( res.current_page >= res.last_page) {
            } else {
                // addSwiper();
            }
        })
    }
})
